﻿using _2301MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _2301MVCDemo.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            //从控制器向视图传值
            //1.字典类型
            ViewData["name"] = "张三";
            //ViewBag.name = "张三";
            //2.对象类型
            ViewBag.age = 20;
            ViewData["sex"] = "男";
            ViewBag.sex = "女";
            //ViewData和ViewBag本质是同一个内容，只是表现形式不一样而已
            //ViewData和ViewBag不能跨越方法

            //3.字典类型 能跨越方法 只能取值一次
            TempData["name"] = "张三";
            TempData["age"] = 20;
            TempData["sex"] = "男";
            //TempData.sex = "男";//错误的

            return View();
        }

        public ActionResult Home()
        {
            return View();
        }

        //第二种：视图向控制器传值 public ActionResult AddUser(string name,string age,string sex)
        //第三种：视图向控制器传值
        public ActionResult AddUser(User model)
        {
            //第一种：视图向控制器传值
            //if (Request.Form["name"] != null)
            //{
            //    string name = Request.Form["name"].ToString();
            //    string age = Request.Form["age"].ToString();
            //    string sex = Request.Form["sex"].ToString();

            //    ViewBag.name = name;
            //    ViewBag.age = age;
            //    ViewBag.sex = sex;
            //}

            //ViewBag.name = name;
            //ViewBag.age = age;
            //ViewBag.sex = sex;

            ViewBag.name = model.Name;
            ViewBag.age = model.Age;
            ViewBag.sex = model.Sex;

            return View();
        }
    }
}